home *** CD-ROM | disk | FTP | other *** search
- Path: risc.uni-linz.ac.at!jsixt
- From: jsixt@risc.uni-linz.ac.at (Johannes Sixt)
- Newsgroups: comp.lang.c++
- Subject: Re: const wasting space and slower?
- Date: 23 Feb 1996 11:52:23 GMT
- Organization: RISC, J.K. University of Linz, Austria
- Distribution: world
- Message-ID: <4gk9pn$r9i@alijku06.edvz.uni-linz.ac.at>
- References: <312814DB.399F@iastate.edu>
- NNTP-Posting-Host: melmac.risc.uni-linz.ac.at
-
- In article <312814DB.399F@iastate.edu>, Steve Lee <sjlee@iastate.edu> writes:
- > When you declare something like this:
- >
- > const int MAX_FILES = 20;
- >
- > doesn't the compiler reserve global, read-only memory for this variable? Or does it just replace
- > instances in the source code where MAX_FILES is found and replaces it with 20, similar to a
- > preprocessor, but with typechecking? Also, if the compiler does place it in memory, do
- > instructions then reference the memory, instead of immediate addressing mode?
-
- According to the Working Paper const variables get "internal linkage" unless
- explicitly declared extern. "internal linkage" means that the variable's name can
- be accessed only within the current compilation unit. "external linkage", OTOH,
- as is assigned to (amongst others) functions by default, would mean that the name
- can be accessed by other compilation units.
-
- Sooo... if nobody outside the current compilation unit is able to access the
- const variable, the compiler has the opportunity to optimize such that no memory
- will be allocated for it. Most probably a compiler will allocate memory and
- reference the memory in non-optimizing compilations, and will not allocate memory
- in high-optimization compilations.
-
- If you define a const variable in a header file, every module which #includes the
- file will have its own copy of the variable. But this is ok, the value is the
- same in all units and can't be changed. If you edit the header and define a
- different value for the variable, you have to make sure that all modules
- referencing the header file get recompiled. But good development environmments
- will do this for you (e.g. Makefiles with proper dependencies).
-
- -- Hannes
-
- PS: Please stick to <80 chars/line!
-